import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import {Button, Card, Col, Row, PageHeader} from "antd";
import OnlFormHeadPresenter from "../../.gen/presenter/OnlFormHeadPresenter";
import {WebPage} from "../../redare_components/webplatform_workbench/utils/WorkbenchDecoratorUtil";
import OnlFormHeadForm from "../../components/onlformhead/OnlFormHeadForm";
import moment from "moment";

const routes = [
    {
        path: '/',
        breadcrumbName: '首页',
    },
    {
        path: '/onlformhead/add',
        breadcrumbName: '添加',
    }
];

export default @WebPage({
    id: 'com.redare.devframework.webplatform.pages.onlformhead.OnlFormHeadFormPage',
    name: '表单添加',
    webPath: '/onlformhead/add',
    webFunIds: []
})

class OnlFormHeadFormPage extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new OnlFormHeadPresenter(this);
    }

    componentDidMount() {
        let {id} = this._urlQuery
        if (id) {
            this.showSpin()
            this.presenter.getOnlFormHead({id}, {}, (result) => {
                if (result && this.form) {
                    let data = result.data;
                    if (data.updateTime) {
                        data.updateTime = moment(data.updateTime);
                    }
                    if (data.createTime) {
                        data.createTime = moment(data.createTime);
                    }
                    this.form.setFormData(data)
                }
            })
        }
    }

    _submitForm() {
        if (this.form) {
            this.form.validateFormFields().then((values) => {
                this.showDoingIndicator();
                if (this._urlQuery.id) {
                    values.id = this._urlQuery.id;
                    this.presenter.editOnlFormHead(values);
                    return;
                }
                this.presenter.addOnlFormHead(values);
            });
        }
    }

    _onEditOnlFormHeadSuccess() {
        this._onAddOnlFormHeadSuccess();
    }

    _onAddOnlFormHeadSuccess() {
        this.routeBack(this.props.history)
        this.sendEvent('onlFormHeadAddOk')
    }

    render() {
        return (
            <div className={'no-calc-footer-height'}>
                <PageHeader onBack={() => this._onBack()} className="bg-white" title={this._urlQuery.id ? '编辑' : '新增'}
                            routes={routes}/>
                {this.renderSpin(
                    <div>
                        <Card bordered={false} className="margin24">
                            <OnlFormHeadForm ref={(o) => {
                                this.form = o
                            }}/>
                        </Card>
                        <div className="footer bg-white">
                            <Row gutter={24} justify="end">
                                <Col>
                                    <Button type="primary"
                                            onClick={this._submitForm.bind(this)}
                                            loading={this.state.submitBtnLoading}>保存</Button>
                                </Col>
                            </Row>
                        </div>
                    </div>
                )}
            </div>
        )
    }

    _onBack() {
        this.routeBack(this.props.history)
    }

    showLoadingIndicator(text, options = {}) {
        this.showSpin(text, options);
    }

    closeLoadingIndicator() {
        this.closeSpin();
    }

    closeDoingIndicator() {
        this.setState({submitBtnLoading: false})
    }

    showDoingIndicator(text, options = {}) {
        this.setState({submitBtnLoading: true})
    }
}





